package com.qby.project.controller;

import com.qby.framework.web.AjaxResult;
import com.qby.project.service.IAppCommonService;
import com.qby.project.service.QnyFileUploadService;
import com.qby.project.vo.MessageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: 公共类业务层
 * @author: qiu bo yang
 * @create: 2020-03-26 10:45
 */
@Api(value = "常用方法", tags = {"常用方法"})
@RestController
@RequestMapping("/app/common")
public class AppCommonController {
    @Autowired
    private IAppCommonService appCommonService;

    @Autowired
    private QnyFileUploadService qnyFileUploadService;

    /**
     * 获取图形验证码
     *
     * @param random
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "获取图形验证码")
    @GetMapping("/getImageCaptcha")
    public void getImageCaptcha(@RequestParam(value = "random") String random, HttpServletResponse response) {
        appCommonService.getImageCaptcha(random, response);
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     * @author qiu bo yang
     */
    @ApiOperation(value = "本地文件上传")
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam(value = "file") MultipartFile file) {
        return appCommonService.uploadFile(file);
    }

    /**
     * 批量上传文件
     *
     * @param files
     * @return
     * @author qiu bo yang
     */
    @ApiOperation(value = "本地批量文件上传")
    @PostMapping("/uploadFiles")
    public AjaxResult uploadFiles(@RequestParam(value = "files") MultipartFile[] files) {
        return appCommonService.uploadFiles(files);
    }

    /**
     * 创瑞发送短信
     *
     * @param messageVo
     * @return
     */
    @ApiOperation(value = "创瑞发送短信")
    @PostMapping("/sendMessage")
    public AjaxResult sendMessage(@RequestBody MessageVo messageVo) {
        return appCommonService.sendMessage(messageVo);
    }

    /**
     * 上传图片 七牛云
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "七牛云上传图片")
    @PostMapping("/qnyUploadImage")
    public AjaxResult qnyUploadImage(@RequestParam(value = "file", required = false) MultipartFile file,
                                     @RequestParam(value = "imageType", required = false) String imageType)
            throws Exception {
        String url = qnyFileUploadService.qnyUploadImage(file, imageType);
        return AjaxResult.success("上传成功", url);
    }
}
